package org.androidpn.client;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Registration;

public class TestConnection {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ConnectionConfiguration connConfig = new ConnectionConfiguration(
				"localhost", 5222);
		// connConfig.setSecurityMode(SecurityMode.disabled);
		connConfig.setSecurityMode(SecurityMode.required);
		connConfig.setSASLAuthenticationEnabled(false);
		connConfig.setCompressionEnabled(false);

		Registration registration = new Registration();

        PacketFilter packetFilter = new AndFilter(new PacketIDFilter(
                registration.getPacketID()), new PacketTypeFilter(
                IQ.class));

        PacketListener packetListener = new PacketListener() {

            public void processPacket(Packet packet) {
                System.out.println("RegisterTask.PacketListener processPacket().....");
                System.out.println("RegisterTask.PacketListener packet="
                        + packet.toXML());

                if (packet instanceof IQ) {
                    IQ response = (IQ) packet;
                    if (response.getType() == IQ.Type.ERROR) {
                        if (!response.getError().toString().contains(
                                "409")) {
                        }
                    } else if (response.getType() == IQ.Type.RESULT) {
                    }
                }
            }
        };
        
		XMPPConnection connection = new XMPPConnection(connConfig);
		connection.addPacketListener(packetListener, packetFilter);
		try {
			// Connect to the server
			connection.connect();
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		Message msg = new Message();
		//msg.setTo("15d69e00070b41afa8289005af46535a@127.0.0.1/AndroidpnClient");
		msg.setTo("broadcast@127.0.0.1/AndroidpnClient");
		msg.setType(Message.Type.chat);
		msg.setBody("32213123123123");
		connection.sendPacket(msg);
	}

}
